package com.example.demo.controller;

import com.example.demo.common.CommonResult;
import com.example.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

/**
 * 文件Controller
 *
 * @author 黄泽涛
 * @date 2021/12/30 16:17
 * @
 **/
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileServce;

    @PostMapping("/fileUpload")
    @ResponseBody
    public CommonResult fileUpload(HttpServletRequest request,
                                   @RequestParam(value = "file", required = false) MultipartFile multipartFile) throws Exception {
//        if(multipartFile.isEmpty()){
//            LOGGER.info("传入文件对象不能为空");
//            return CommonResult.failed("传入文件对象不能为空");
//        }
//        if(multipartFile.getSize() > 1024*1024*10){
//            LOGGER.info("传入文件不能超过10M");
//            return CommonResult.failed("传入文件不能超过10M");
//        }
        CommonResult commonResult = fileServce.fileUpload(multipartFile);
        return commonResult;
    }

    @GetMapping("/fileDownLoad")
    @ResponseBody
    public CommonResult fileDownLoad(HttpServletResponse response, @RequestParam("fileName") String fileName){

        return fileServce.fileDownLoad(fileName,response);
    }


    @GetMapping("/getFileInformation")
    @ResponseBody
    public CommonResult getFileInformation(@RequestParam("fileName") String fileName){
        return fileServce.getFileInformation(fileName);
    }
}
